package de.ba.se.roboter.ui;

import java.awt.Component;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;

import de.ba.se.roboter.controller.MainController;
import de.ba.se.roboter.data.Orienation;
import de.ba.se.roboter.data.Robot;

/**
 * GUI für die Konfiguration des {@link Robot}.
 * 
 * @author Sebastian Tönse
 */
public class ConfigView extends JDialog {
	
	/** @see java.io.Serializable */
	private static final long serialVersionUID = -9206735685355200088L;
	
	private final JLabel orientationLabel = new JLabel("Richtung:");

	private JComboBox<Orienation> orientationComboBox;
	
	private final JLabel widthLabel = new JLabel("Feldbreite:");
	
	private JSpinner widthSpinner;
	
	private final JLabel heightLabel = new JLabel("Feldhöhe:");
	
	private JSpinner heightSpinner;
	
	private JButton confirmButton;
	
	private MainController controller;
	
	/**
	 * Der Konstruktor, der benutzt wird.
	 * @param owner Das Eltern-Fenster.
	 * @param controller Der Controller, der die Events dieser GUI empfängt.
	 */
	public ConfigView(JFrame owner, MainController controller) {
		super(owner);
		this.controller = controller;
		setModal(true);
		setTitle("Roboter konfigurieren");
		final GridLayout layout = new GridLayout(4, 2, 5, 10);
		setLayout(layout);
		add(orientationLabel);
		add(getOrientationComboBox());
		add(widthLabel);
		add(getWidthSpinner());
		add(heightLabel);
		add(getHeightSpinner());
		add(getConfirmButton());
		pack();
		setLocationRelativeTo(owner);
		setVisible(true);
	}

	/**
	 * Die Auswahlbox, um die Startrichtung zu wählen.
	 * @return Die Auswahlbox.
	 */
	public JComboBox<Orienation> getOrientationComboBox() {
		if (orientationComboBox == null) {
			orientationComboBox = new JComboBox<Orienation>();
			final DefaultComboBoxModel<Orienation> model = new DefaultComboBoxModel<Orienation>(new Orienation[] { Orienation.NORTH, Orienation.EAST, Orienation.SOUTH, Orienation.WEST });
			orientationComboBox.setModel(model);
			orientationComboBox.setRenderer(new DefaultListCellRenderer() {
				
				/** @see java.io.Serializable */
				private static final long serialVersionUID = 6704573995781032632L;

				@Override
				public Component getListCellRendererComponent(JList<? extends Object> list,
						Object value, int index, boolean isSelected, boolean cellHasFocus) {
					if (value instanceof Orienation) {
						switch ((Orienation)value) {
						case NORTH:
							setText("oben");
							break;
						case EAST:
							setText("links");
							break;
						case SOUTH:
							setText("unten");
							break;
						case WEST:
							setText("rechts");
							break;
						}
					}
					return this;
				}
			});
		}
		return orientationComboBox;
	}

	/**
	 * Einstellung der Feldbreite.
	 * @return Das Element.
	 */
	public JSpinner getWidthSpinner() {
		if (widthSpinner == null) {
			widthSpinner = new JSpinner();
			widthSpinner.setModel(new SpinnerNumberModel(10, 0, Integer.MAX_VALUE, 1));
		}
		return widthSpinner;
	}

	/**
	 * Einstellung der Feldhöhe.
	 * @return Das Element.
	 */
	public JSpinner getHeightSpinner() {
		if (heightSpinner == null) {
			heightSpinner = new JSpinner();
			heightSpinner.setModel(new SpinnerNumberModel(10, 0, Integer.MAX_VALUE, 1));
		}
		return heightSpinner;
	}

	/**
	 * Der Button zum Bestätigen der Konfiguration.
	 * @return Das Element.
	 */
	public JButton getConfirmButton() {
		if (confirmButton == null) {
			confirmButton = new JButton("Bestätigen");
			confirmButton.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent e) {
					final Orienation o = getOrientationComboBox().getItemAt(getOrientationComboBox().getSelectedIndex());
					final int width = Integer.parseInt(getWidthSpinner().getValue().toString());
					final int height = Integer.parseInt(getHeightSpinner().getValue().toString());
					controller.setConfiguration(o, width, height);
					dispose();
				}
			});
		}
		return confirmButton;
	}
}
